<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>博客编辑</title>
	
		<link rel="stylesheet" href="css/conmmon.css">
		<link rel="stylesheet" href="css/blog_edit.css">
	
		<link rel="stylesheet" href="editor.md/css/editormd.min.css" />
		<script src="js/jquery.min.js"></script>
		<script src="editor.md/editormd.js"></script>
		<script src="js/urlutils.js"></script>
		<script src="js/logout.js"></script>
	</head>
	<body>
		<div class="nav">
			<img src="img/logo2.jpg" alt="">
			<span class="title">我的博客系统</span>
			<span class="spacer"></span>
			<a href="blog_list.html">博客论坛</a>
			<a href="myblog_list.html">我的博客</a>
			<a href="javascript:logout()">注销</a>
		</div>
		<div class="blog-edit-container">
			<div class="title">
				<input id="title" type="text" placeholder="在这里写下文章标题">
				<button onclick="doUpdate()">修改文章</button>
			</div>
			<div id="editorDiv">
				<textarea id="editor-markdown" style="display:none;"></textarea>
			</div>
		</div>
		<script>
			var id=getParamValue("id");
			var editor;
			function initEdit(md){
				editor = editormd("editorDiv", {
					width: "100%",
					height: "calc(100% - 50px)",
					markdown: md,
					path: "editor.md/lib/",
					saveHTMLToTextarea: true
				});
			}
			initEdit("# 在这里写下一篇博客");
			
			function init(){
				if(id==null||id<=0){
					alert("非法参数");
					return false;
				}
				jQuery.ajax({
					url:"/art/init_update",
					type:"GET",
					data:{
						"id":id
					},
					success:function(res){
						if(res.code==200&&res.data!=null&&res.data.id>0){
							jQuery("#title").val(res.data.title);
							initEdit(res.data.content);
						}else if(res.code==-2){
							alert("查询失败："+res.msg);
							location.href="login.html";
						}else{
							alert("查询失败："+res.msg);
						}
					}
				});
			}
			init();
			
			function doUpdate(){
				var title=jQuery("#title");
				if(title.val().trim()==""){
					alert("请输入标题");
					title.focus();
					return false;
				}
				if(editor.getValue()==""){
					alert("请输入内容");
					return false;
				}
				jQuery.ajax({
					url:"/art/update",
					type:"POST",
					data:{
						"id":id,
						"title":title.val(),
						"content":editor.getValue()
					},
					success:function(res){
						if(res.code==200&&res.data==1){
							alert("修改成功");
							location.href="myblog_list.html";
						}else if(res.code==-2){
							alert("操作失败："+res.msg);
							location.href="login.html";
						}else{
							alert("操作失败："+res.msg);
						}
					}
				});
			}
		</script>
	</body>
</html>